/* 
Style attributes:
	arrowSize
*/
	
package UIFree.controls
{
	import UIFree.uif_app;
	import UIFree.uif_component;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class uif_hint extends uif_baseHint {
		protected var _curFocus	: uif_component;
		protected var _eventID	: int;
		
		public function uif_hint(parent:uif_component, ax:int=0, ay:int=0, 
								awidth:int=0, aheight:int=0, styleClass:String='', mousePos: Boolean = true) {
			super(parent, ax, ay, awidth, aheight, styleClass);
			mouseChildren 	= false;
			mouseEnabled 	= false;
			textField.mouseEnabled = false;
			visible = false;
			if (mousePos) {
				app.addEventListener(MouseEvent.MOUSE_MOVE, function(e: MouseEvent): void {
					This.position = parent.mousePos.subtract(This.hintPos());
				});
			}
			app.addEventListener(uif_app.HINT_OVER, function(e: MouseEvent): void {
				if (This.curFocus != e.target) This.curFocus = e.target as uif_component;
			});			

			app.addEventListener(uif_app.HINT_OUT, function(e: MouseEvent): void {
				if (This.curFocus) This.curFocus = null;
			});
		}
		
		
		override public function setPositionInParent(a_x:Number, a_y:Number): void {
			if (parentComponent) {
				var inner: Rectangle = parentComponent.getInner();
				if (a_x < inner.x) a_x = inner.x;
				else if (a_x + width > inner.x + inner.width) a_x = inner.x + inner.width - width;
				if (a_y + height > inner.y + inner.height) a_y = inner.y + inner.height - height;
				doSetPosition(a_x, a_y);
			}
		}
		
		override public function setPosition(a_x:Number, a_y:Number): void {
			setPositionInParent(a_x, a_y);
		}
		
		public function get curFocus() : uif_component {
			return _curFocus;
		}
		
		public function set curFocus(a_curFocus : uif_component): void {
			if (_curFocus) removeEvent(_eventID);
			_curFocus = a_curFocus;
			if (_curFocus && _curFocus.hint && (_curFocus.hint is String)) {
				htmlText = _curFocus.hint as String;
				height = textField.textHeight + getSpace(true) * 2;
				slideShow();	
				_eventID = addEvent(uif_component.UPDATEHINT, doUpdateHint, _curFocus);
			} else {
				slideHide();
			}
		}
		
		protected function doUpdateHint(e: Event): void {
			htmlText = (e.target as uif_component).hint as String;
		}

	}
}